package alittlepond.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

import alittlepond.event.Notifiable;
import alittlepond.world.World;
import alittlepond.world.World.WorldAction;
import alittlepond.world.WorldProperty;


/**
 * Swing visualisation of the World and everything in it.
 * 
 * Handles the redrawing itself I guess.
 * 
 * @author hopey
 *
 */
@SuppressWarnings("serial")
public class WorldWindow  extends JFrame implements Notifiable{

	
	World world = World.INSTANCE;
	private Dimension worldDimension;
	private JComboBox clickActionSelect;
	public void init() {
		if(!world.isInitialised()) throw new IllegalStateException("World not initialised");
		world.addUpdateListener(this);
		
		worldDimension = new Dimension(world.getWidth(), world.getHeight());
		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
//		setPreferredSize(worldDimension);
		setTitle("The World");
		
		
		add(new JPanel(){
			@Override
			protected void paintComponent(Graphics g) {
				g.drawImage(world.getImage(), 0,0, 300,300,this);
			}
			{
			setPreferredSize(worldDimension);
			addMouseListener(new MouseAdapter() {

				@Override
				public void mouseClicked(MouseEvent e) {
					World.WorldAction action = (WorldAction) clickActionSelect.getSelectedItem();
					world.clickAction(action, e.getX(), e.getY());
				}
				
			});
			
			
		}}, BorderLayout.CENTER);

		clickActionSelect = new JComboBox(World.WorldAction.values()){{
			setSelectedIndex(0);
			setPreferredSize(Form.FORM_ITEM_SIZE);
			setMaximumSize(Form.FORM_ITEM_SIZE);
		}};
		
		JPanel rightPanel = new JPanel() {{
			setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		
			add(new Form<WorldProperty>(world));
			
			add(clickActionSelect);
		}};
		add(rightPanel,BorderLayout.EAST);
		pack();
		setVisible(true);
	}
	@Override
	public void update() {
		repaint();
	}
	
}
